package com.itsci.assignment2;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class TravelManager extends SQLiteOpenHelper{
	public TravelManager(Context context, String name, CursorFactory factory,
			int version) {
		super(context, "Travel", null, version);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE Travel(travelId INTEGER  PRIMARY KEY AUTOINCREMENT ,NAME TEXT,price INTEGER,Des TEXT);");
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE Travel");
		onCreate(db);
		
	}
	
	public void insertTravel(TravleBean t) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues content = new ContentValues();
		content.put("name", t.getName());
		content.put("price", t.getPrice());
		content.put("des",t.getdes());
		db.insert("Travel", null, content);
		db.close();
	}
	
	public void updateTravel(TravleBean t) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues content = new ContentValues();
		content.put("name", t.getName());
		content.put("price", t.getPrice());
		content.put("des",t.getdes());
		db.update("Travel", content, "travelId = ?", new String[] { t.getId()+"" });
		db.close();
	}
	
	public void deleteTravel(int id) {
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete("Travel", "travelId = ?", new String[] { id+"" });
		db.close();
	}
	
	public int getId(String name){
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.query("Travel", new String[] { "TravelId","name","price","des"},
				"name = ?", new String[] { name }, null, null, null);
		int id = 0;
		if (cursor.moveToFirst()) {
		 id = Integer.parseInt(cursor.getString(0));
		}
		
		return id;
	}
	
	public TravleBean getByTravelId(String name) {
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.query("Travel", new String[] { "TravelId","name","price","des"},
				"name = ?", new String[] { name }, null, null, null);
		TravleBean v = new TravleBean();
		if (cursor.moveToFirst()) {
			v.setName(cursor.getString(1));
			v.setPrice(Integer.parseInt(cursor.getString(2)));
			v.setdes(cursor.getString(3));
		} else {
			v.setName("");
			v.setPrice(0);
			v.setdes("");
	
		}
		db.close();
		return v;
	}
	
	public ArrayList<TravleBean> getAllTravel() {
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.query("Travel", new String[] {"travelId","name","price","des"}, null, null, null, null, null);
		ArrayList<TravleBean> arrayTravel = new ArrayList<TravleBean>();
		if (cursor.moveToFirst()) {
			do {
				TravleBean v = new TravleBean();				
				v.setName(cursor.getString(1));
				v.setPrice(Integer.parseInt(cursor.getString(2)));
				v.setdes(cursor.getString(3));
				arrayTravel.add(v);
			}while(cursor.moveToNext());
		}
		db.close();
		return arrayTravel;
	}
}
